convert from readFileStrict
authorJoey Hess <joeyh@joeyh.name>
Wed, 22 Jan 2025 19:19:47 +0000 (15:19 -0400)
committerJoey Hess <joeyh@joeyh.name>
Wed, 22 Jan 2025 19:23:33 +0000 (15:23 -0400)
In cases where it reads lines, which needs newline conversion on
windows.

Assistant/Install.hs
Assistant/Ssh.hs
Config/Smudge.hs
Logs/File.hs
Upgrade/V7.hs
Utility/Misc.hs

index 94a40369cc67800fc1e7b6f4153474798994ac58..b2b7b91c625332c02b7447d60612e301611b2d2d 100644 (file)
@@ -17,6 +17,7 @@ import Utility.Shell
 import Utility.Tmp
 import Utility.Env
 import Utility.SshConfig
+import qualified Utility.FileIO as F
 
 #ifdef darwin_HOST_OS
 import Utility.OSX
@@ -126,17 +127,18 @@ installFileManagerHooks program = unlessM osAndroid $ do
                (kdeDesktopFile actions)
   where
        genNautilusScript scriptdir action =
-               installscript (scriptdir </> scriptname action) $ unlines
+               installscript (toRawFilePath (scriptdir </> scriptname action)) $ unlines
                        [ shebang
                        , autoaddedcomment
                        , "exec " ++ program ++ " " ++ action ++ " --notify-start --notify-finish -- \"$@\""
                        ]
        scriptname action = "git-annex " ++ action
        installscript f c = whenM (safetoinstallscript f) $ do
-               writeFile f c
-               modifyFileMode (toRawFilePath f) $ addModes [ownerExecuteMode]
+               writeFile (fromRawFilePath f) c
+               modifyFileMode f $ addModes [ownerExecuteMode]
        safetoinstallscript f = catchDefaultIO True $
-               elem autoaddedcomment . lines <$> readFileStrict f
+               elem (encodeBS autoaddedcomment) . fileLines'
+                       <$> F.readFile' (toOsPath f)
        autoaddedcomment = "# " ++ autoaddedmsg ++ " (To disable, chmod 600 this file.)"
        autoaddedmsg = "Automatically added by git-annex, do not edit."
 
index a85b2945776e09baaf0d5cf50d5ff797dc1817d6..3a9235c76d88201752a3f9ec30c98c3354def6dd 100644 (file)
@@ -17,6 +17,7 @@ import Utility.SshConfig
 import Git.Remote
 import Utility.SshHost
 import Utility.Process.Transcript
+import qualified Utility.FileIO as F
 
 import Data.Text (Text)
 import qualified Data.Text as T
@@ -158,9 +159,9 @@ removeAuthorizedKeys :: Bool -> FilePath -> SshPubKey -> IO ()
 removeAuthorizedKeys gitannexshellonly dir pubkey = do
        let keyline = authorizedKeysLine gitannexshellonly dir pubkey
        sshdir <- sshDir
-       let keyfile = sshdir </> "authorized_keys"
-       tryWhenExists (lines <$> readFileStrict keyfile) >>= \case
-               Just ls -> viaTmp writeSshConfig (toOsPath (toRawFilePath keyfile)) $
+       let keyfile = toOsPath $ toRawFilePath $ sshdir </> "authorized_keys"
+       tryWhenExists (map decodeBS . fileLines' <$> F.readFile' keyfile) >>= \case
+               Just ls -> viaTmp writeSshConfig keyfile $
                        unlines $ filter (/= keyline) ls
                Nothing -> noop
 
index da198096fe53573ece673322d9d7990787dc6627..7d880c05cf3783dff0ffcc9965ab4281cd29bcb3 100644 (file)
@@ -17,6 +17,7 @@ import Git.Types
 import Config
 import Utility.Directory.Create
 import Annex.Version
+import qualified Utility.FileIO as F
 
 import qualified System.FilePath.ByteString as P
 
@@ -65,9 +66,10 @@ stdattr =
 -- git-annex does not commit that.
 deconfigureSmudgeFilter :: Annex ()
 deconfigureSmudgeFilter = do
-       lf <- fromRawFilePath <$> Annex.fromRepo Git.attributesLocal
-       ls <- liftIO $ catchDefaultIO [] $ lines <$> readFileStrict lf
-       liftIO $ writeFile lf $ unlines $
+       lf <- Annex.fromRepo Git.attributesLocal
+       ls <- liftIO $ catchDefaultIO [] $ 
+               map decodeBS . fileLines' <$> F.readFile' (toOsPath lf)
+       liftIO $ writeFile (fromRawFilePath lf) $ unlines $
                filter (\l -> l `notElem` stdattr && not (null l)) ls
        unsetConfig (ConfigKey "filter.annex.smudge")
        unsetConfig (ConfigKey "filter.annex.clean")
index f385b06d665de7e807eef9fa9737b738c570a61a..93aef17f97be7519811cf63e28a95ed9fff4781d 100644 (file)
@@ -28,8 +28,6 @@ import Annex.ReplaceFile
 import Utility.Tmp
 import qualified Utility.FileIO as F
 
-import qualified Data.ByteString as S
-import qualified Data.ByteString.Char8 as S8
 import qualified Data.ByteString.Lazy as L
 import qualified Data.ByteString.Lazy.Char8 as L8
 
@@ -158,32 +156,3 @@ createDirWhenNeeded f a = a `catchNonAsync` \_e -> do
        -- done if writing the file fails.
        createAnnexDirectory (parentDir f)
        a
-
--- On windows, readFile does NewlineMode translation,
--- stripping CR before LF. When converting to ByteString,
--- use this to emulate that.
-fileLines :: L.ByteString -> [L.ByteString]
-#ifdef mingw32_HOST_OS
-fileLines = map stripCR . L8.lines
-  where
-       stripCR b = case L8.unsnoc b of
-               Nothing -> b
-               Just (b', e)
-                       | e == '\r' -> b'
-                       | otherwise -> b
-#else
-fileLines = L8.lines
-#endif
-
-fileLines' :: S.ByteString -> [S.ByteString]
-#ifdef mingw32_HOST_OS
-fileLines' = map stripCR . S8.lines
-  where
-       stripCR b = case S8.unsnoc b of
-               Nothing -> b
-               Just (b', e)
-                       | e == '\r' -> b'
-                       | otherwise -> b
-#else
-fileLines' = S8.lines
-#endif
index cad16f18544b05eaadeb2c6a9fa906611e2be601..0e301bd09de7e3fcdc4dbea1084a1e8fc5b282ac 100644 (file)
@@ -22,6 +22,7 @@ import qualified Git
 import Git.FilePath
 import Config
 import qualified Utility.RawFilePath as R
+import qualified Utility.FileIO as F
 
 import qualified System.FilePath.ByteString as P
 import System.PosixCompat.Files (isSymbolicLink)
@@ -127,11 +128,12 @@ populateKeysDb = unlessM isBareRepo $ do
 -- checked into the repository.
 updateSmudgeFilter :: Annex ()
 updateSmudgeFilter = do
-       lf <- fromRawFilePath <$> Annex.fromRepo Git.attributesLocal
-       ls <- liftIO $ lines <$> catchDefaultIO "" (readFileStrict lf)
+       lf <- Annex.fromRepo Git.attributesLocal
+       ls <- liftIO $ map decodeBS . fileLines'
+               <$> catchDefaultIO "" (F.readFile' (toOsPath lf))
        let ls' = removedotfilter ls
        when (ls /= ls') $
-               liftIO $ writeFile lf (unlines ls')
+               liftIO $ writeFile (fromRawFilePath lf) (unlines ls')
   where
        removedotfilter ("* filter=annex":".* !filter":rest) =
                "* filter=annex" : removedotfilter rest
index 7c00a184f4f701872a20d48065cc38d74c9455c9..d30cce25242b87e3047e446e279be47958362ca9 100644 (file)
@@ -5,6 +5,7 @@
  - License: BSD-2-clause
  -}
 
+{-# LANGUAGE CPP #-}
 {-# OPTIONS_GHC -fno-warn-tabs #-}
 
 module Utility.Misc (
@@ -15,6 +16,8 @@ module Utility.Misc (
        separateEnd',
        firstLine,
        firstLine',
+       fileLines,
+       fileLines',
        segment,
        segmentDelim,
        massReplace,
@@ -32,6 +35,9 @@ import Data.List
 import System.Exit
 import Control.Applicative
 import qualified Data.ByteString as S
+import qualified Data.ByteString.Char8 as S8
+import qualified Data.ByteString.Lazy as L
+import qualified Data.ByteString.Lazy.Char8 as L8
 import Prelude
 
 {- A version of hgetContents that is not lazy. Ensures file is 
@@ -78,6 +84,35 @@ firstLine' = S.takeWhile (/= nl)
   where
        nl = fromIntegral (ord '\n')
 
+-- On windows, readFile does NewlineMode translation,
+-- stripping CR before LF. When converting to ByteString,
+-- use this to emulate that.
+fileLines :: L.ByteString -> [L.ByteString]
+#ifdef mingw32_HOST_OS
+fileLines = map stripCR . L8.lines
+  where
+       stripCR b = case L8.unsnoc b of
+               Nothing -> b
+               Just (b', e)
+                       | e == '\r' -> b'
+                       | otherwise -> b
+#else
+fileLines = L8.lines
+#endif
+
+fileLines' :: S.ByteString -> [S.ByteString]
+#ifdef mingw32_HOST_OS
+fileLines' = map stripCR . S8.lines
+  where
+       stripCR b = case S8.unsnoc b of
+               Nothing -> b
+               Just (b', e)
+                       | e == '\r' -> b'
+                       | otherwise -> b
+#else
+fileLines' = S8.lines
+#endif
+
 {- Splits a list into segments that are delimited by items matching
  - a predicate. (The delimiters are not included in the segments.)
  - Segments may be empty. -}